﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Framework;

namespace SchemaMonitor.json
{
	public class CSoftPost : CSoft
	{
		public CSoftPost(CNodeBasic root, CUser cache) : base(root, cache)
		{
		}

		public string PhotoIds_ { get { return CUtilities.CountSummary(PhotoIds, "photo", string.Empty); } }

		public List<long> PhotoIds { get { return GetListLong("photoIds"); } }
		public CSoftUser From { get { return GetUser("from", _cache); } }
		public string Message { get { return GetStr("message"); } }
		public string Name { get { return GetStr("name"); } }
		public string Caption { get { return GetStr("caption"); } }
		public CSoftCommentList Comments { get { return GetComments(); } }
		public string Picture { get { return GetStr("picture"); } }
		public string Description { get { return GetStr("description"); } }
		public string Attachments { get { return GetStr("attachments"); } }
		public string ObjectId { get { return GetStr("object_id"); } }
		public string Type { get { return GetStr("type"); } }
		public string Privacy { get { return PrivacyString; } }
		public string Story { get { return GetStr("story"); } }
		public string FullPicture { get { return GetStr("full_picture"); } }

		public CSoftPhoto GetPhoto(long id)
		{
			if (!HasAttachments)
				return null;
			foreach (var i in Attachments_)
				if (i.Target.HasValue && i.Target.Value == id)
					return i;
			return null;
		}

		public CNodeLabelList Labels { get { return CNodeLabel.Cache.GetByNodeGuid(Root_.Guid); } }
		public CNodeLabels_OneToOneList Labels_O2O { get { return CNodeLabels_OneToOne.Cache.GetByNodeGuid(Root_.Guid); } }
		public CMetaTagList Metatags { get { var list = new CMetaTagList(); list.AddRange(Labels.Metatags); list.AddRange(Labels_O2O.Metatags); return list; } }

		public CMetaTagList RemainingMetaTags() { return RemainingMetaTags(CTagType.Cache.FB_Post); }
		public CMetaTagList RemainingMetaTags(CTagType type)
		{
			var temp = new CMetaTagList(type.MetaTags);
			foreach (var i in this.Metatags)
				temp.Remove(i);
			return temp;
		}


		public bool HasAttachments { get { return Fields.Has("attachments") && Fields["attachments"].AsCompositeList.Count > 0; } }
		public bool HasSubAttachments { get { return HasAttachments && Attachments__[0].Fields.Has("subattachments"); } }
		private List<CNodeBasic> Attachments__ { get { return HasAttachments ? Fields["attachments"].AsCompositeList : null; } }
		private List<CNodeBasic> SubAttachments__ { get { return  HasSubAttachments ? Attachments__[0].Fields["subattachments"].AsCompositeList : null; } }
		public List<CSoftPhoto> Attachments_
		{
			get
			{
				if (!HasAttachments)
					return null;

				var a = Attachments__;
				var list = new List<CSoftPhoto>();
				if (HasSubAttachments)
				{
					var s = SubAttachments__;
					foreach (var i in s)
						list.Add(new CSoftPhoto(i, _cache));
					return list;
				}
	
				list.Add(new CSoftPhoto(a[0], _cache));
				return list;
			}
		}

		public int CountAttachments
		{
			get
			{
				if (!Fields.Has("attachments"))
					return 0;

				var ff = Fields["attachments"].AsCompositeList;
				int count = 0;
				foreach (var i in ff)
					count += CountAttach(i);
				return count;
			}
		}
		private int CountAttach(CNodeBasic r)
		{
			int count = 0;
			if (! r.Fields.Has("subattachments"))
				return count;
			var ff = r.Fields["subattachments"].AsCompositeList;
			count += ff.Count;
			return count;
		}
	}
}
